# -*- coding: UTF-8 -*-

from .BaseService import BaseService

class WechatUserLoginOutService(BaseService):

    def logout(self, uin):
        mUserInfoDetail = self.core.loader.instance('do', 'UserinfoDetail')
        userinfoDetailDO = mUserInfoDetail.get(uin)
        if userinfoDetailDO is None:
            return self.core.lib.outputobj(False, '登出发生异常：用户登录信息获取失败')

        url = '%s/webwxlogout' % userinfoDetailDO.login_url
        params = {
            'redirect' : 1,
            'type' : 1,
            'skey' : userinfoDetailDO.skey
        }
        request = self.core.loader.lib('request', transport_core = True)
        responseRes = request.sGet(mSession = userinfoDetailDO.session, url = url, params = params, headers = self.core.config.DEFAULT_HEADER)
        if not responseRes.success:
            return self.core.lib.outputobj(False, '登出发生异常：%s' % responseRes.message)

        mUserInfoDetail.remove(uin)
        del userinfoDetailDO
        return self.core.lib.outputobj(True, '登出成功')
